#include "iBe.h"

Scene::DrawFunc::DrawFunc(const RenderPtr _render):
  render(_render)
{}

void Scene::DrawFunc::operator() (NamedObjectSet<ObjectPtr>::ValueType _val)
{
  render->pushMatrix();
  render->setMatrix( _val.second->location, _val.second->orientation );
  render->draw( _val.second->mesh );
  render->popMatrix();
}

Scene::UpdateFunc::UpdateFunc(const UINT64 &_time):time(_time)
{}


void Scene::UpdateFunc::operator() (NamedObjectSet<ObjectPtr>::ValueType _val)
{
  _val.second->update(time);
}

Scene::Scene()
{}

Scene::~Scene()
{}

bool Scene::update(const UINT64 &_time)
{
  UpdateFunc func(_time);

  objectSet.foreach(func);

  return false;
}

void Scene::addObject( ObjectPtr _object )
{
  if (_object)
    objectSet.insert( _object );
}

void Scene::setCamera( const Turple3f &_eye, const Turple3f &_center, const Turple3f &_up )
{
  eye = _eye;
  center = _center;
  up = _up;
}

void Scene::draw( RenderPtr &_render )
{
  DrawFunc func(_render);

  _render->begin();
  _render->setCamera(eye, center, up);

  objectSet.foreach(func);

  _render->end();

}
